<?php

class Agence
{
    protected $db;
    public $template;
    public $lang;
    public $session;
    public $setting;
    public $smarty;
    public $cache;
    
    public function __construct($paramClass)
    {
        if(is_array($paramClass))
        {
            foreach($paramClass as $key => $value)
            {
                $this->$key = $value;
            } 
        }     
    }
    
    public function liste_index()
    {
        $Query = "SELECT * FROM immo_agence ORDER BY nom";
        $Sql = $this->db->sql_query($Query);
        
        $agences = array();
        
        while($Agence = $this->db->sql_fetcharray($Sql))
        {
        	$Agence['action'] = '
        		<a href="agence_edition.php?agence_id='. $Agence['id'] .'" title=""><img src="images/edit.png"  style="border:none; width:20px; height:20px;" /></a>
        	   &nbsp;&nbsp;&nbsp;
               <a href="javascript:delagence(\'' . $Agence['id'] . '\');"><img src="images/del.png" style="border:none; width:20px; height:20px;" /></a>';
        	
            array_push($agences,$Agence);
        }
        
        $this->smarty->assign('agences', $agences);
        $this->smarty->assign('lang', $this->lang);
        $this->smarty->assign('token', $this->session->_session['token']);
        
        $this->smarty->display('agence.tpl');  
    }
    
    /**
     * @desc Ajout l agence dans la base de donnee
     * */
    public function ajout()
    {
        $error = 0;
        
        if(!$nom = retrieve('POST', 'nom', true, true)) $error++;
        if(!$adresse = retrieve('POST', 'adresse', true, true)) $error++;
        $adresse_suite = retrieve('POST', 'adresse_suite', false, true);
        if(!$codepostal = retrieve('POST', 'code_postal', true, true)) $error++;
        if(!$ville = retrieve('POST', 'ville', true, true)) $error++;
        $telephone = retrieve('POST', 'telephone', false, true);
        $fax = retrieve('POST', 'fax', false, true);
        $email = retrieve('POST', 'email', false, true);
        if(!email_valid($email)) $email = '';
        
        if($error > 0) redirect('agence_ajout.php?error=formulaire_error');
        
        if(!$photo_up = upload_file('photo', IMMOPHP_ROOT_PATH . 'upload/agences/')) $photo_up = '';
        
        $ArrayQuery = array(
        'nom'	=>	$nom,
        'adresse'	=>	$adresse,
        'adresse_suite'	=>	$adresse_suite,
        'codepostal'	=>	$codepostal,
        'ville'	=>	$ville,
        'telephone'	=>	$telephone,
        'fax'	=>	$fax,
        'email'	=>	$email,
        'photo'	=>	$photo_up);	
        
        $this->db->sql_query(CreateQueryInsert($ArrayQuery, 'immo_agence'));
        
        $this->smarty->assign('error_msg', true);
        $this->smarty->assign('error_class', 'error_success');
        $this->smarty->assign('error_image', 'comment_ok');
        $this->smarty->assign('error_message',$this->lang['Agence_ajoutee']);
        $this->smarty->assign('url_site',$this->setting['url_site']);
    }
    
    /**
     * @desc Modifie l agence dans la base en fonction de donnee passer par $_POST
     * @return void
     * */
    public function edit()
    {
        $error = 0;
    	$photo_up = '';
    	
    	if(!$id = retrieve('POST', 'agence_id', true)) $error++;
        if(! $this->check_id($id) ) $error++;
    	if(!$nom = retrieve('POST', 'nom', true, true)) $error++;
    	if(!$adresse = retrieve('POST', 'adresse', true, true)) $error++;
    	$adresse_suite = retrieve('POST', 'adresse_suite', false, true);
    	if(!$codepostal = retrieve('POST', 'code_postal', true, true)) $error++;
    	if(!$ville = retrieve('POST', 'ville', true, true)) $error++;
    	$telephone = retrieve('POST', 'telephone', false, true);
    	$fax = retrieve('POST', 'fax', false, true);
    	$email = retrieve('POST', 'email', false, true);	
    	if(!email_valid($email)) $email = '';	
    	if($error > 0) redirect('agence.php?error=champs_error');
    	
    	// Traitement image
    	if(!$photo_up = upload_file('photo', '../upload/agences/')) $photo_up = '';
    	if(isset($_POST['old_photo']) && $photo_up != '') unlink('../upload/agences/' . $_POST['old_photo']);
    	if(isset($_POST['old_photo']) && $photo_up == '') $photo_up = $_POST['old_photo'];    	
    	
    	$ArrayQuery = array(
    	'nom'	=>	$nom,
    	'adresse'	=>	$adresse,
    	'adresse_suite'	=>	$adresse_suite,
    	'codepostal'	=>	$codepostal,
    	'ville'	=>	$ville,
    	'telephone'	=>	$telephone,
    	'fax'	=>	$fax,
    	'email'	=>	$email,
    	'photo' => $photo_up);	
    	
    	$this->db->sql_query(CreateQueryUpdate($ArrayQuery, 'immo_agence', 'id = '. $id));
        
        $this->smarty->assign('error_msg', true);
        $this->smarty->assign('error_class', 'error_success');
        $this->smarty->assign('error_image', 'comment_ok');
        $this->smarty->assign('error_message',$this->lang['Agence_modifiee']);
        $this->smarty->assign('url_site',$this->setting['url_site']);
    }
    
    public function delete()
    {
        if( !$agence_id = retrieve('GET', 'agence_id', true) ) redirect('agence.php');      // Recuperation de l agence
        if( !$this->check_id($agence_id) ) redirect('agence.php');                                 // Verification format agence
        if( !$this->session->csrf_check_get() ) redirect('logout.php?error=csrf');          // Verification du jeton
        if( !$this->check_annonce_agence($agence_id) ) return false;                        // Verification si annonce lie a cette agence
        
        // Recuperation nom de la photo
        $agence = $this->db->sql_query("SELECT photo FROM immo_agence WHERE id = '". $agence_id ."'", 'ARRAY');
        
        if( $agence['photo'] != '' )
        {
            // Supression photo
            @unlink('../upload/agences/' . $agence['photo']);
        }
        
        // Supression de l agence
        $this->db->sql_query("DELETE FROM immo_agence WHERE id = '". $agence_id ."'");
        
        $this->smarty->assign('error_msg', true);
        $this->smarty->assign('error_class', 'error_success');
        $this->smarty->assign('error_image', 'comment_ok');
        $this->smarty->assign('error_message',$this->lang['Agence_supprimee']);
        $this->smarty->assign('url_site',$this->setting['url_site']);
        
        return true;        
    }
    
    public function check_id($id)
    {
        if(! is_numeric($id) ){
            return false;
        }   else {
            return true;
        }
    }
    
    /**
     * @desc verifie la presence d annonce lie a cette agence
     * @param $agence_id int 
     * @return true|false
     * */
    public function check_annonce_agence($agence_id)
    {
        $annonce = $this->db->sql_query("SELECT id FROM immo_annonce WHERE agence = '". $agence_id ."'");
        
        if($this->db->nb_resultat > 0)
        {
            // Affichage message
            $this->smarty->assign('error_msg', true);
            $this->smarty->assign('error_class', 'error_warning');
            $this->smarty->assign('error_image', 'comment_warning');
            $this->smarty->assign('error_message',$this->lang['Agence_non_supprime_car_annonce']);
            $this->smarty->assign('url_site',$this->setting['url_site']);
            
            return false;
        }
        else
        {
            return true;
        }
    }
}